<?php
	require_once("configuration.php");
	require_once("includes/db/dbsetup.php");
	require_once("includes/MySmarty.class.php");
class Rate{
	var $xmlFile;
	function Rate($xmlFile){
		$this->xmlFile = $xmlFile;
	}
	function getXML(){
		 $c = curl_init($this->xmlFile);
		 curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
		 $page = curl_exec($c);
		 curl_close($c);
			//$xml = file_get_contents($this->source);*/
			return $page;
		//return file_get_contents($this->xmlFile);
	}
	function getRate(){
		$xmlData = NULL;
		$p = xml_parser_create();
		xml_parse_into_struct($p,$this->getXML() , $xmlData);
		xml_parser_free($p);
		$data = array('date'=>$xmlData[1]['value']);
		if($xmlData){
			foreach($xmlData as $v) if(isset($v['attributes'])) $data[] = $v['attributes'];
			return $data;
		}
		return false;
	}
	function show(){
		$data = $this->getRate();
		$i = 0;
		if($data)
		{
			
			foreach($data as $k=>$v)
			{
				
				if($v['CODE']=='EUR' || $v['CODE']=='GBP' || $v['CODE']=='JPY' || $v['CODE']=='SGD' || $v['CODE']=='USD')
				{
					$i++;
					if($k!='date')
					{
					
						if($i%2 != 0)
						{
							$str .= '<tr class="white">';
						}
						else
						{
							$str .= '<tr class="grey">'; 
						}
						
						if($v['BUY']==0)
						{
							$v['BUY'] = "-";
						}
						
						$str .= '<td>'.$v['CODE'].'</td>'.'<td>'.$v['BUY'].'</td>'.'<td>'.$v['TRANSFER'].'</td>'.'<td>'.$v['SELL'].'</td>';
						$str .= '</tr>';
					}
				}
			}
		}
		return $str;
	}
	
	function showall(){
		$data = $this->getRate();
		$i = 0;
		if($data)
		{
			
			foreach($data as $k=>$v)
			{
				
				
					$i++;
					if($k!='date')
					{
					
						if($i%2 != 0)
						{
							$strall .= '<tr class="white">';
						}
						else
						{
							$strall .= '<tr class="grey">'; 
						}
						
						if($v['BUY']==0)
						{
							$v['BUY'] = "-";
						}
						
						$strall .= '<td>'.$v['CODE'].'</td>'.'<td>'.$v['NAME'].'</td>'.'<td>'.$v['BUY'].'</td>'.'<td>'.$v['TRANSFER'].'</td>'.'<td>'.$v['SELL'].'</td>';
						$strall .= '</tr>';
					}
			}
		}
		return $strall;
	}
}
$rate = new Rate('http://canvietmy.com/module/generate-rate.php');
$result_rate = $rate->show();
$result_rate_all = $rate->showall();
$smarty->assign("RATE", $result_rate);
$smarty->assign("RATEALL", $result_rate_all);
?>
